-
-
Notifications
You must be signed in to change notification settings - Fork 360
Match bubble sort C implementation with Julia #254
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
@@ -1,42 +1,36 @@ | |||
#include <stdio.h> | |||
#include <stddef.h> |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I know I did it but you don't need that library. size_t
i in stdio.h
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
extra include is removed.
} | ||
} | ||
|
||
int main() { | ||
int array[10] = {1, 45, 756, 4569, 56, 3, 8, 5, -10, -4}; | ||
const int N = 10; | ||
int array[N] = {1, 45, 756, 4569, 56, 3, 8, 5, -10, -4}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The code doesn't run because of the N
here. You need just put int array[10] = ...
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Couldn't you just use int array[] = { ... }
? I tried that and got no errors whatsoever. You could then determine the value of N
with sizeof
.
Disclaimer: I'm not a C programmer.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, and also, wouldn't size_t
be more fitting because it describes the size of the array?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can do both. I would change int to size_t
, I just didn't notice. On the first point, use int array[] = { ... }
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My bad, I forgot it is C, I need to do #define
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think deriving N using sizeof is better than doing #define. It make code dryer, with #define, information about size of array used independently in 2 places: it is embedded in declaration of array and in #define, in case of sizeof it is only in the array.
I re-pushed changes with sizeof.
} | ||
} | ||
|
||
int main() { | ||
int array[10] = {1, 45, 756, 4569, 56, 3, 8, 5, -10, -4}; | ||
int array[] = {1, 45, 756, 4569, 56, 3, 8, 5, -10, -4}; | ||
size_t N = sizeof(array) / sizeof(*array); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why not just have size_t N = 10;
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It will make code less stable, if you make a mistake and assign 100 to N it
will crash.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm asking you to set N to 10, that will not create any issues.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Gathros That's reduntant, though. There is only one correct value for N and that's the length of the array. You can calculate that, so why hardcode it? That way, you can add/remove numbers from the array without having to worry about changing N
, too.
No description provided.